﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using com.fmtx.Model;
using com.fmtx.IDal;
using fmtx.Framework.DALFactory;
using fmtx.Utility;

namespace website.Areas.Admin.Controllers
{
    public class ArticleController : AdminBaseController
    {
        //
        // GET: /Admin/Article/
        IArticle _article = DataAccess<IArticle>.GetObject();
        ICity _city = DataAccess<ICity>.GetObject();
        IPlace _place = DataAccess<IPlace>.GetObject();

        public ActionResult Index(string title,string city,string place,string uname,Approved statu=Approved.AllStatu,int uid=0,int cityid=0,int placeid=0,int index=0)
        {
            int count = 0;
            var models = _article.Query(index, 12, title,statu, uid, cityid, placeid, out count);
            ViewBag.count = count;
            return View(models);
        }

        /// <summary>
        /// 编辑内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var model = _article.Get(id);
            return View(model);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Article model)
        {
            if (ModelState.IsValid)
            {
                var omodel = _article.Get(model.Id);
                omodel.Title = model.Title;
                string firstimg = model.FrontCover;
                model.Content = StaticCs.SaveHtmlWord(model.Content, ref firstimg);
                if (string.IsNullOrEmpty(omodel.FrontCover) && !string.IsNullOrEmpty(firstimg))
                {
                    model.FrontCover = firstimg;
                    omodel.FrontCover = model.FrontCover;
                    omodel.Scale = ImageFactory.GetScale(Server.MapPath(StaticCs.VirtualPath + firstimg));
                }
                omodel.Content = model.Content;
                omodel.Summary = omodel.Content.RemoveHtml(200);
                omodel.KeyWord = model.KeyWord;
                _article.Update(omodel);
                StaticCs.RemoveUserFolder(userInfo.Uid);
                return RedirectToAction("Index");
            }
            return View(model);
        }
    }
}
